package com.sui.bigdata.sml.web.misc.k8s;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author songhaicheng
 * @date 2020/3/26 10:29
 * @description K8S 的组件变换状态
 * @reviewer
 */
public class Evolve {

    private int numCount;
    private AtomicInteger numCurrent;
    private AtomicBoolean done;
    private AtomicBoolean success;

    public Evolve(int numCount) {
        this.numCount = numCount;
        this.numCurrent = new AtomicInteger(0);
        this.done = new AtomicBoolean(false);
        this.success = new AtomicBoolean(false);
    }

    public void currentPlus() {
        numCurrent.getAndIncrement();
        if (numCurrent.get() == numCount) {
            success.compareAndSet(false, true);
            done.compareAndSet(false, true);
        }
    }

    /**
     * 手动设置成功状态，一般用于某一 entity 失败时手动调用
     * @param success
     */
    public void setSuccess(boolean success) {
        this.success.set(success);
        this.done.set(true);
    }

    public boolean isSuccess() {
        return success.get();
    }

    public boolean isDone() {
        return done.get();
    }

}
